package fr.cdj.thebox.client.content.ui;

import java.util.Date;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;

import fr.cdj.thebox.client.resources.TheboxBundle;
import fr.cdj.thebox.client.utils.Checkbox;
import fr.cdj.thebox.client.utils.Icon;
import fr.cdj.thebox.shared.FileMetaData;
import fr.cdj.thebox.shared.FileType;

public class FileItemDisplayImpl extends FlowPanel implements FileItemDisplay {

	private HorizontalPanel fileItem;

	private Icon icon;
	private Checkbox checkbox;
	private Label nameLabel;
	private Label lengthLabel;
	private Label lastModifiedLabel;

	private Icon loadingIcon;
	
	private FlowPanel fileItemsContainer;

	private final String DATE_TIME_PATTEN = "dd/MM/yyyy hh:mm";
	private final DateTimeFormat DATE_TIME_FORMAT = DateTimeFormat.getFormat(DATE_TIME_PATTEN);

	@Inject
	public FileItemDisplayImpl(TheboxBundle resources, Provider<Icon> iconProvider, Provider<Checkbox> checkboxProvider,
			@Assisted FileMetaData fileMetaData) {

		fileItem = new HorizontalPanel();
		fileItem.setStyleName(resources.getStyle().fileItem());

		if (fileMetaData.isDirectory()) {
			fileItem.addStyleName(resources.getStyle().directory());
		} else {
			fileItem.addStyleName(resources.getStyle().file());
		}

		if(fileMetaData.isHidden()) {
			fileItem.addStyleName(resources.getStyle().hidden());
		}
		
		icon = iconProvider.get();
		icon.addStyleName(resources.getStyle().fileItemIcon());
		
		String name = fileMetaData.getName();
		FileType typeFromExt = FileType.getTypeFromExt(name.substring(name.lastIndexOf('.') + 1).toLowerCase());
		if (typeFromExt != null) {
			fileItem.getElement().setAttribute("type", typeFromExt.name());
		}
		
		checkbox = checkboxProvider.get();
		checkbox.addStyleName(resources.getStyle().fileItemCheckbox());
		nameLabel = new Label(fileMetaData.getName());
		nameLabel.setStyleName(resources.getStyle().fileItemName());
		lengthLabel = new Label("-");
		lengthLabel.setStyleName(resources.getStyle().fileItemLength());
		if (fileMetaData.getLength() != null) {
			lengthLabel.setText(formatLength(fileMetaData.getLength()));
		}
		lastModifiedLabel = new Label("-");
		lastModifiedLabel.setStyleName(resources.getStyle().fileItemLastModified());
		if (fileMetaData.getLastModified() != null) {
			lastModifiedLabel.setText(DATE_TIME_FORMAT.format(new Date(fileMetaData.getLastModified())));
		}

		fileItem.clear();
		fileItem.add(icon);
		fileItem.add(checkbox);
		fileItem.add(nameLabel);
		fileItem.setCellWidth(nameLabel, "100%");
		fileItem.add(lengthLabel);
		fileItem.add(lastModifiedLabel);

		loadingIcon = iconProvider.get();
		loadingIcon.addStyleName(resources.getStyle().loading());
		
		fileItemsContainer = new FlowPanel();
		fileItemsContainer.setStyleName(resources.getStyle().fileItemContainer());

		this.add(fileItem);
		this.add(fileItemsContainer);
	}

	private String formatLength(Long length) {
		Long tmplength = length;
		String dec = "";
		String type = " bytes";
		if (tmplength / 1000 > 0) {
			dec = "," + (tmplength % 1000);
			tmplength = tmplength / 1000;
			type = "kb";
			if (tmplength / 1000 > 0) {
				dec = "," + (tmplength % 1000);
				tmplength = tmplength / 1000;
				type = "Mb";
				if (tmplength / 1000 > 0) {
					dec = "," + (tmplength % 1000);
					tmplength = tmplength / 1000;
					type = "Gb";
				}
			}
		}
		return tmplength + dec + type;
	}

	@Override
	public HasClickHandlers getHasItemClickHandlers() {
		return nameLabel;
	}
	
	public void setOpened(boolean opened) {
		if (opened) {
			fileItem.getElement().setAttribute("open", "");
			fileItemsContainer.getElement().setAttribute("open", "");
		} else {
			fileItem.getElement().removeAttribute("open");
			fileItemsContainer.getElement().removeAttribute("open");
		}
	}

	public void setLoading() {
		fileItemsContainer.clear();
		fileItemsContainer.add(loadingIcon);
	}
	
	@Override
	public void addFileItem(FileItemDisplay display) {
		fileItemsContainer.add(display.asWidget());
	}

	@Override
	public void clearItems() {
		fileItemsContainer.clear();
	}

	@Override
	public HasValue<Boolean> getHasSelectedValue() {
		return checkbox;
	}

}
